package com.hnyyac.ad.mds.views_load

import android.app.Activity
import android.content.Context
import android.os.Bundle
import android.util.Log
import com.bytedance.sdk.openadsdk.AdSlot
import com.bytedance.sdk.openadsdk.TTAdConstant
import com.bytedance.sdk.openadsdk.TTAdNative
import com.bytedance.sdk.openadsdk.TTAdSdk
import com.bytedance.sdk.openadsdk.TTRewardVideoAd
import com.bytedance.sdk.openadsdk.mediation.MediationConstant
import com.bytedance.sdk.openadsdk.mediation.ad.MediationAdSlot
import com.bytedance.sdk.openadsdk.mediation.manager.MediationBaseManager
import com.hnyyac.ad.mds.GMAds
import com.hnyyac.ad.mds.IAdType
import com.hnyyac.ad.mds.IAdView
import com.hnyyac.ad.mds.IAdViewListener
import com.hnyyac.ad.mds.KadControler
import com.hnyyac.ad.mds.LLogs
import com.hnyyac.ad.mds.cache.CacheAdLoadedManager


class NewGromoreRewardedVideoAd(
    val context: Activity, val showId: String,
    override var ecpmAdnShowIdListStr: String? = null,
    override var showChainSession: String? = null,
    var initialPosition: Int =0 // 新增构造参数
) : IAdView {
    private val TAG = "qin"
    private var mRewardVideoAd: TTRewardVideoAd? = null
    private var mUAdViewShowListener: IAdViewListener? = null
    val adNativeLoader: TTAdNative
    val adslot: AdSlot

    override fun getPosition(): Int {
        return initialPosition
    }

    override fun getSdkName(): String? {
        return mRewardVideoAd?.mediationManager?.showEcpm?.sdkName.toString()
    }

    override fun setPosition(position: Int) {
        this.initialPosition = position
    }

    init {
        adNativeLoader = TTAdSdk.getAdManager().createAdNative(context) //需要传activity，切记！！！
        adslot = AdSlot.Builder()
            .setCodeId(showId)
            .setOrientation(TTAdConstant.VERTICAL) //横竖屏设置
            .setMediationAdSlot(
                MediationAdSlot.Builder()
                    .setExtraObject(
                        MediationConstant.ADN_PANGLE,
                        "pangleRewardCustomData"
                    ) //服务端奖励验证透传参数
                    .setExtraObject(MediationConstant.ADN_GDT, "gdtRewardCustomData")
                    .setExtraObject(MediationConstant.ADN_BAIDU, "baiduRewardCustomData")
                    .build()
            )
            .build()


    }
    override fun loadAd() {
        Log.i("qin","开始拿广告")
        GMAds.iAdViewListener?.onTrigger(this@NewGromoreRewardedVideoAd)  //触发广告
        adNativeLoader.loadRewardVideoAd(adslot, object : TTAdNative.RewardVideoAdListener {
            override fun onError(i: Int, s: String) {
                Log.i("qin","拿广广告onError")
                GMAds.iAdViewListener?.onError(s.toString())
            }

            override fun onRewardVideoAdLoad(ttRewardVideoAd: TTRewardVideoAd) {
                mRewardVideoAd = ttRewardVideoAd
                if (mRewardVideoAd?.mediationManager?.isReady!!) {
                    //Log.i("qin","拿视频广告成功")
                    KadControler.pushUAdViewCache(this@NewGromoreRewardedVideoAd)
                    //   PreLoadHelper.pushPreLoadAd(this@NewGromoreRewardedVideoAd)
                    GMAds.iAdViewListener?.onLoad(this@NewGromoreRewardedVideoAd)
                } else {
                    //Log.i("qin","拿视频广告失败")
                    mUAdViewShowListener?.onLoadError(this@NewGromoreRewardedVideoAd)
                    CacheAdLoadedManager.onAdLoadFail(this@NewGromoreRewardedVideoAd)
                    GMAds.iAdViewListener?.onLoadError(this@NewGromoreRewardedVideoAd)


                }
            }

            override fun onRewardVideoCached() {
            }

            override fun onRewardVideoCached(ttRewardVideoAd: TTRewardVideoAd) {
                mRewardVideoAd = ttRewardVideoAd
                //可在该回调后进行广告展示
            }
        })
        CacheAdLoadedManager.onAdStartLoad(this)
    }

    override fun isValid(): Boolean {
        return mRewardVideoAd?.mediationManager?.isReady!!
    }

    override fun getshowid_bg(): String {
        return mRewardVideoAd?.mediationManager?.showEcpm?.requestId.toString()
    }

    override fun getAdType(): IAdType {
        return IAdType.reward_video
    }

    override fun getEcpm(): Double {
        kotlin.runCatching {
            return getEcpm_chapin(mRewardVideoAd!!).toDouble()
        }
        return 0.0
    }

    override fun show(activity: Activity) {
        this.mRewardVideoAd?.setRewardAdInteractionListener(object :
            TTRewardVideoAd.RewardAdInteractionListener {
            override fun onAdShow() {
                mUAdViewShowListener?.onAdShow(this@NewGromoreRewardedVideoAd)
                GMAds.iAdViewListener?.onAdShow(this@NewGromoreRewardedVideoAd)

            }

            override fun onAdVideoBarClick() {
                mUAdViewShowListener?.onAdClick(this@NewGromoreRewardedVideoAd)
                GMAds.iAdViewListener?.onAdClick(this@NewGromoreRewardedVideoAd)
            }

            override fun onAdClose() {
                mUAdViewShowListener?.onAdClose(this@NewGromoreRewardedVideoAd)
                GMAds.iAdViewListener?.onAdClose(this@NewGromoreRewardedVideoAd)
            }

            override fun onVideoComplete() {

            }

            override fun onVideoError() {
            }

            //已废弃，请使用onRewardArrived方法
            override fun onRewardVerify(p0: Boolean, p1: Int, p2: String?, p3: Int, p4: String?) {

            }

            override fun onRewardArrived(p0: Boolean, p1: Int, p2: Bundle?) {
                mUAdViewShowListener?.onSuccess(this@NewGromoreRewardedVideoAd)
                GMAds.iAdViewListener?.onSuccess(this@NewGromoreRewardedVideoAd)
            }

            override fun onSkippedVideo() {
                mUAdViewShowListener?.onSkipped()
                GMAds.iAdViewListener?.onSkipped()
            }
        })
        mRewardVideoAd?.showRewardVideoAd(activity);
    }

    override fun getAdShowId(): String {
        return showId
    }

    override fun getSlotId(): String {
        return mRewardVideoAd?.mediationManager?.showEcpm?.slotId.toString()
    }

    override fun toString(): String {

        return ""

    }

    override fun setOnShowingListener(listener: IAdViewListener?) {
        this.mUAdViewShowListener = listener
    }



    /**
     * 获取激励广告广告的ecpm
     */
    private fun getEcpm_chapin(ad: TTRewardVideoAd): String {
        val mediationManager: MediationBaseManager? = ad.mediationManager
        if (mediationManager != null) {
            val showEcpm = mediationManager.showEcpm
            if (showEcpm != null) {
                return showEcpm.ecpm
            }
        }
        return "0.0"
    }
}